import app from '../App.vue'
// import {
// 	inject
// } from "@vue/runtime-core"



// console.log("serviceUrl", app.expose.globalData);

// let serviceUrl = "http://yayixun.top"

let serviceUrl = "https://min.yayixun.top"



function request(options) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: serviceUrl + options.url,
			data: options.data,
			header: options.header,
			method: options.method,
			sslVerify: false,
			success: (res) => {
				if (res.data.code == 0) {
					resolve(res.data)
				} else if (res.data.code == 401) {
					uni.reLaunch({
						url: '/pages/login/index'
					})
					return
				} else if (res.data.code == 500) {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					return false
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
					return
				}
			},
			fail: (res) => {
				// uni.showToast({
				// 	title: i18n.t("index.requireNextTips"),
				// 	icon: 'error'
				// })
			},
			complete: (res) => {}
		})
	}).then((res) => {
		return res
	})
}


function get(e) {
	let url = e.url;
	let data = e.data;
	let header = {
		'Content-Type': 'application/x-www-form-urlencoded',
		Authorization: uni.getStorageSync('token') || '',
		'tenant-id': 163
	};
	let options = {
		url: url,
		data: data,
		header: header,
		method: 'GET'
	}
	return request(options)
};

function postXXX(e) {
	let url = e.url;
	let data = e.data;
	let header = {
		'Content-Type': 'application/x-www-form-urlencoded',
		Authorization: uni.getStorageSync('token') || '',
		'tenant-id': 163
	};
	let options = {
		url: url,
		data: data,
		header: header,
		method: 'POST'
	}
	return request(options)
};

function post(e) {
	let url = e.url;
	let data = e.data;
	let header = {
		'Content-Type': 'application/json',
		Authorization: uni.getStorageSync('token') || '',
		'tenant-id': 163
	};
	let options = {
		url: url,
		data: JSON.stringify(data),
		header: header,
		method: 'POST'
	}
	return request(options)
};

function postJson(e) {
	let url = e.url;
	let data = e.data;
	let header = {
		'Content-Type': 'application/json',
		Authorization: uni.getStorageSync('token') || '',
		'tenant-id': 163
	};
	let options = {
		url: url,
		data: data,
		header: header,
		method: 'POST'
	}
	return request(options)
};

function mapGet(e) {
	let url = e.url;
	let data = e.data;
	return new Promise((resolve, reject) => {
		uni.request({
			url: url,
			data: data,
			method: 'GET',
			sslVerify: false,
			success: (res) => {
				resolve(res)
			},
			fail: (res) => {
				// uni.showToast({
				// 	title: i18n.t("index.requireNextTips"),
				// 	icon: 'error'
				// })
			},
			complete: (res) => {}
		})
	}).then((res) => {
		return res
	})
};


export default {
	post,
	get,
	postXXX,
	postJson,
	mapGet
}